package com.petRescueCenter.demo.voluntary.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.petRescueCenter.demo.user.entity.User;
import com.petRescueCenter.demo.voluntary.dao.UserVoluntaryDao;
import com.petRescueCenter.demo.voluntary.dao.VolunteerDao;
import com.petRescueCenter.demo.voluntary.entity.UserVoluntary;
import com.petRescueCenter.demo.voluntary.entity.Volunteer;
import com.petRescueCenter.demo.voluntary.entity.DTO.VolunteerDTO;

@Service
@Transactional
public class VolunteerService implements IVolunteerService {
	@Autowired
	private VolunteerDao volunteerDao;
	@Autowired
	private UserVoluntaryDao userVoluntaryDao;

	@Override
	public Volunteer save(Volunteer entity) {
		// TODO Auto-generated method stub
		return volunteerDao.save(entity);
	}

	@Override
	public Volunteer findById(Long id) {
		// TODO Auto-generated method stub
		return volunteerDao.findById(id).get();
	}

	@Override
	public boolean existsById(Long id) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public long count() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public void deleteById(Long id) {
		// TODO Auto-generated method stub
		volunteerDao.deleteById(id);
	}

	@Override
	public void deleteAll(Long[] ids) {
		// TODO Auto-generated method stub
	}

	@Override
	public Page<VolunteerDTO> findAll(Specification<Volunteer> spec, Pageable pageable) {
		// TODO Auto-generated method stub
		Page<Volunteer> pages = volunteerDao.findAll(spec, pageable);
		List<VolunteerDTO> Lists = null;
		if (pages != null) {
			Lists = new ArrayList<VolunteerDTO>();
			for (Volunteer page : pages) {
				VolunteerDTO dto = new VolunteerDTO();
				dto.entityToDto(page, dto);
				Lists.add(dto);
			}
		}
		return new PageImpl<VolunteerDTO>(Lists, pageable, pages.getTotalElements());
	}

	@Override
	public boolean existsByName(String name) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public Volunteer findByUserId(Long id) {
		// TODO Auto-generated method stub
		return volunteerDao.findByUserId(id);
	}

	@Override
	public Page<VolunteerDTO> getVolunteer(Specification<UserVoluntary> spec, Pageable pageable) {
		Page<UserVoluntary> pages = userVoluntaryDao.findAll(spec, pageable);
		List<VolunteerDTO> Lists = null;
		if (pages != null) {
			Lists = new ArrayList<VolunteerDTO>();
			for (UserVoluntary entity : pages) {
				VolunteerDTO dto = new VolunteerDTO();
				dto.UserVoluntaryToVolunteerDTO(entity, dto);
				Lists.add(dto);
			}
		}
		return new PageImpl<VolunteerDTO>(Lists, pageable, pages.getTotalElements());
	}

	@Override
	public User findByName(String name) {
		// TODO Auto-generated method stub
		return null;
	}

}
